package com.tarim.streetdance.service.impl;

import com.tarim.streetdance.mapper.UserMapper;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.model.VideoWithBLOBs;
import com.tarim.streetdance.service.UserService;
import com.tarim.streetdance.util.AESUtils;
import com.tarim.streetdance.util.MD5Utils;
import com.tarim.streetdance.vo.Criteria;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User insertSelective(User record) {
        record.setDr(0);
        record.setLoginErrorNum(0);
        record.setStatus(0);
        record.setCreateTime(new Date());
        record.setDisableSend(0);
        record.setUserType(1);
        record.setUserName(record.getUserCode());
        record.setSex(0);
        int i = userMapper.insertSelective(record);
        return record;
    }

    @Override
    public List<User> selectByCondition(User record) {
        return userMapper.selectByCondition(record);
    }

    @Override
    public User selectForLogin(User record) {
        record.addExtField("loginName", record.getUserCode());
        return userMapper.selectForLogin(record);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        record.setModifyTime(new Date());
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        user.setLoginErrorNum(null);
        user.setPassword(null);
        return user;
    }

    @Override
    public PageBean<User> selectByQueryCondition(Criteria criteria, PageBean<User> pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex()*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<User> list = userMapper.selectByQueryCondition(criteria);
        Long count = userMapper.selectByQueryConditionCount(criteria);
        pageBean.setTotalCount(count);
        pageBean.setData(list.toArray());
        return pageBean;
    }
}